# 导入包
from matplotlib import pyplot as plt
import pandas as pd
import statsmodels.formula.api as smf
from sqlalchemy import create_engine
import pymysql
import statsmodels.api as sm
import numpy as np
import seaborn as sns

db_config = {
    'host': 'localhost',
    'user': 'root',
    'password': 'sjk1234',
    'database': 'tu',
    'port': 3306,
    'charset': 'sys'   # 添加字符集设置
}
engine = create_engine(
    f"mysql+pymysql://{db_config['user']}:{db_config['password']}@{db_config['host']}:{db_config['port']}/{db_config['database']}?charset={db_config['charset']}"
)
conn = pymysql.connect(**db_config)
chunk_size = 10000
df = pd.read_sql_query("""
    SELECT d.* FROM date_1 d WHERE d.trade_date BETWEEN '2023-01-01' and '2023-12-31' and d.ts_code = '000001.SZ'
    """,
    conn,
    chunksize=chunk_size)
df1 = pd.concat(df, ignore_index=True)

# 新增股票涨跌
df1['zd_closes'] = round((df1['closes'] - df1['closes'].shift(1)) / df1['closes'].shift(1), 2)
df1['zs_closes'] = round((df1['i_vloses'] - df1['i_vloses'].shift(1)) / df1['i_vloses'].shift(1), 2)
df1['zs_vol'] = round((df1['i_vol'] - df1['i_vol'].shift(1)) / df1['i_vol'].shift(1), 2)
print(df1.head())

# 处理缺失值数据
df1 = df1.dropna(subset=['zd_closes', 'zs_closes', 'zs_closes'])
print(df1.head())

# 筛选自变量
ex = ['zd_closes', 'id', 'ts_code', 'trade_date', 'the_date', 'opens', 'high', 'low', 'closes', 'pre_closes', 'change']
number = df1.select_dtypes(include=['number']).columns.tolist()
newlist = [col for col in number if col not in ex]

